import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2023-03-02
 * Time: 19:48
 */

//练习二
public class ListBook {
    @SuppressWarnings({"all"})
    //使用List的实现类添加三本书 并遍历 打印如下效果 打印的顺序是在 toString 重写中实现的
    //名称:××   价格:××   作者:××
    //名称:××   价格:××   作者:××
    //名称:××   价格:××   作者:××
    //1.价格从低到高 (使用冒泡排序)
    //2.要求使用ArrayList、LinkedList、Vector集合实现
    public static void main(String[] args) {
        Book book1 = new Book("三国演绎",200,"罗贯中");
        Book book2 = new Book("西游记",50,"吴承恩");
        Book book3 = new Book("刻意练习",88,"安德斯");

        List list = new ArrayList();
        //后面的是实现类 直接替换就行
        list.add(book1);
        list.add(book2);
        list.add(book3);

        for (Object o : list) {
            System.out.println(o);
        }

        //冒泡排序之后
        sort(list);
        System.out.println("--------冒泡排序价格之后--------");
        for (Object o :list) {
            System.out.println(o);
        }

    }
    //如何对 集合 进行排序
    public static void sort(List list){
        //遍历的过程中比较 冒泡排序
        for (int i = 0; i < list.size()-1; i++) {
            for (int j = 0; j < list.size()-1-i; j++) {
                //取出book
                Book book1 = (Book) list.get(j); //向下转型
                Book book2 = (Book) list.get(j+1);
                if (book1.getPrice() > book2.getPrice()){
                    //交换
                    list.set(j,book2);
                    list.set(j+1,book1);
                }
            }
        }
    }

}


class Book{
    private String name;
    private int price;
    private String author; //啊sir 作者

    public Book(String name, int price, String author) {
        this.name = name;
        this.price = price;
        this.author = author;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    //打印的顺序是在 toString 重写中实现的
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", author='" + author + '\'' +
                '}';
    }
}